package br.unicamp.mc715.common;

import java.io.Serializable;

import br.unicamp.mc715.transport.TransportId;

// import br.unicamp.mc715.transport.Message;

public class Clock implements Serializable, Comparable<Clock> {
	private static final long serialVersionUID = -7690650624114775950L;

	private Integer c;
	private TransportId proc;
	
	public Clock(TransportId proc)
	{
		c = new Integer(0);
		this.proc = proc;
	}
	
	public TransportId getId()
	{
		return proc;
	}
	
	public void setId(TransportId id)
	{
		this.proc = id;
	}

	public void resetClock() {
		c = 0;
	}

	public void setClock(Integer ac) {
		c = ac;
	}

	public void tictac() {
		c++;
	}

	public void tictac(Clock lc) {
		if (lt(lc)) {
			c = lc.c;
		}
		c++;
	}
	
	public String toString() {
		return proc+"-"+c;
	}

	public boolean eq(Clock lc) {
		return (c == lc.c && proc.equals(lc.proc));
	}

	public boolean gt(Clock lc) {
		return (c > lc.c || (c == lc.c && proc.compareTo(lc.proc) > 0));
	}

	public boolean lt(Clock lc) {
		return (c < lc.c || (c == lc.c && proc.compareTo(lc.proc) < 0));
	}

	public int compareTo(Clock o) {
		if (lt(o)) return -1;
		else if (gt(o)) return 1;
		else return 0;
	}

// 	public void timestampMessage(Message m) {
// 		tictac();
// 		m.lc.setClock(c);
// 	}

}
